// 导入 axios 导入各种Axios的TS类型
import axios, {
  type AxiosRequestConfig,
  type AxiosResponse,
  type InternalAxiosRequestConfig,
} from 'axios'

const instance = axios.create({
  // baseURL: 'http://localhost:3000/api',
})

//请求拦截器
instance.interceptors.request.use((config: InternalAxiosRequestConfig<any>) => {
  //添加登录信息
  return config
})

//相应拦截器
instance.interceptors.response.use(
  (res: AxiosResponse) => {
    //对返回结果进行判断
    return res
  },
  (err) => {
    return Promise.reject(err)
  },
)

function request<T>(config: AxiosRequestConfig): Promise<T> {
  return new Promise((resolve, reject) => {
    instance
      .request<any, AxiosResponse<T>>(config)
      .then((response: AxiosResponse<T>) => {
        const data = response.data
        resolve(data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export interface NetResponse {
  [key: string]: any
}

export default request
